package com.bdqn.crm;

import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SysUserService;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTemplateTester {
    @Resource
    private SysUserService sysUserService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;  //操作字符串数据

    @Resource
    private RedisTemplate redisTemplate; //操作其他数据类型

    @Test
    public void testString() {
        stringRedisTemplate.opsForValue().set("name", "czkt");  //操作字符串
        Assert.assertEquals("czkt", stringRedisTemplate.opsForValue().get("name"));
        String name = stringRedisTemplate.opsForValue().get("name");
        System.out.println(name);
//        stringRedisTemplate.opsForValue().append("name","czkt");
//        String t=stringRedisTemplate.opsForValue().get("name");
//        System.out.println(t);
//        stringRedisTemplate.delete("name");  //删除某个建
//        String t=stringRedisTemplate.opsForValue().get("name");

    }

    @Test
    public void test2() {
        SysUser sysUser = new SysUser("czkt", "123456",1L,null);
        ValueOperations<String, SysUser> operations = redisTemplate.opsForValue();
        operations.set("names", sysUser);
        SysUser user = operations.get("names");
        System.out.println("user.getUsrName:" + user.getUsrName());
        String s = stringRedisTemplate.opsForValue().get("name");
        System.out.println(s);
    }

    @Test
    public void testExpire() throws InterruptedException {
        SysUser sysUser = new SysUser("czkt", "123456", 1L, null);
        ValueOperations<String, SysUser> operations = redisTemplate.opsForValue();
        operations.set("expire", sysUser, 100, TimeUnit.MILLISECONDS);
        Thread.sleep(1000);
        boolean exists = redisTemplate.hasKey("expire");
        if (exists) {
            System.out.println("exists is true");
        } else {
            System.out.println("exists is false");
        }
    }
    @Test
    /**
     * 操作Hash数据类型
     */
    public void testHash(){
        HashOperations<String,Object,Object> hash = redisTemplate.opsForHash();
        hash.put("hash","name","ktjiaoyu");
        String value = (String) hash.get("hash","name");
        System.out.println("hash name value:"+value);
    }
    @Test
    public void testList(){
        ListOperations<String,String> list = redisTemplate.opsForList();
        list.leftPush("list","accp");
        list.leftPush("list","bdqn");
        list.leftPush("list","czkt");
        String value = list.leftPop("list");
        System.out.println("list value："+value.toString());
    }
    @Test
    public void testGetSysUser(){
        SysUser sysUser = sysUserService.getSysUser(2L);
        System.out.println("user.usrName:"+sysUser.getUsrName());
    }
}
